package com.landian.demo.fastJson.demo02;

import java.util.List;

import com.alibaba.fastjson.JSON;

/**
 * https://www.cnblogs.com/whatlonelytear/p/5152121.html
 */
class FastjsonDemo {
    public static void main(String[] args) {
        Cat cat1 = new Cat();
        cat1.setColor("white");

        Cat cat2 = new Cat();
        cat2.setColor("black");

        Master master = new Master();
        master.setName("bobo");
        master.getCats().add(cat1);
        master.getCats().add(cat2);

        // 主人对象转json串
        String masterJson = JSON.toJSONString(master);
        System.out.println("master json : " + masterJson);
        // json串转主人对象
        Master masterObj = JSON.parseObject(masterJson, Master.class);
        System.out.println("master object:" + masterObj);

        // 构建用户对象数组
        Cat[] cats = new Cat[2];
        cats[0] = cat1;
        cats[1] = cat2;
        // 数组转JSON串
        String jsonString2 = JSON.toJSONString(cats);
        System.out.println("array String:" + jsonString2);
        // JSON串转
        List<Cat> users2 = JSON.parseArray(jsonString2, Cat.class);
        System.out.println("array obj:" + users2);

    }
}